package com.hyf.meterialdesign.activity;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hyf.meterialdesign.R;
import com.hyf.meterialdesign.base.BaseAppBarActivity;

public class ToolbarTitleCenterActivity extends BaseAppBarActivity {

    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_toolbar_title_center);
        initView();
        initData();
    }

    private void initData() {
        initToolBar();
    }

    private void initToolBar() {
        setToolbarTitle(toolbar,"这是标题呢",Color.YELLOW);
        Drawable drawable = getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp);
        drawable.setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_IN);
        toolbar.setNavigationIcon(drawable);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Drawable overflowIcon = toolbar.getOverflowIcon();
        overflowIcon.setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_IN);
        toolbar.setOverflowIcon(overflowIcon);
        toolbar.inflateMenu(R.menu.menu_appbarlayout_activity);

        //设置toolbar的高度和paddingtop
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) toolbar.getLayoutParams();
        params.height += ToolbarTintActivity.getStatusBarHeight(this);
        toolbar.setLayoutParams(params);
        toolbar.setPadding(0,ToolbarTintActivity.getStatusBarHeight(this),0,0);
    }

    private void setToolbarTitle(Toolbar toolbar,String title,int titleColor){
        if (toolbar != null){
            toolbar.removeAllViews();
            //设置标题
            toolbar.setTitle("");
            TextView textView = new TextView(this);
            textView.setText(title);
            textView.setTextColor(titleColor);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
            Toolbar.LayoutParams params = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT,Toolbar.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER;
            toolbar.addView(textView,params);
        }
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
    }
}
